<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View};
use think\exception\ValidateException;

use app\common\model\{Project};
use app\common\validate\{ProjectValidate};

/** 项目管理 */
class ProjectController extends BaseController
{
    private $userModel;
    public function __construct(Project $projectModel)
    {
        parent::initialize();
        $this->projectModel = $projectModel;
    }

    /** 渲染项目列表页面 */
    public function project_list()
    {
        return view();
    }

    /** 项目列表数据列表 */
    public function project_data()
    {
        $data = input('get.');

        try{
            validate(ProjectValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        /** 查询数据总条数 */
        $count = $this->projectModel->getNum($where)['data'];

        /** 查询列表 */
        $list = $this->projectModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }
}
